package com.dkd.manage.service.Impl;

import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.constant.StatusConstant;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.pojo.dto.VmAddDto;
import com.dkd.manage.pojo.dto.VmQueryListDto;
import com.dkd.manage.pojo.entity.Channel;
import com.dkd.manage.pojo.entity.Node;
import com.dkd.manage.pojo.entity.VendingMachine;
import com.dkd.manage.pojo.entity.VmType;
import com.dkd.manage.pojo.vo.VendingMachineVo;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.VmService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class VmServiceImpl implements VmService {

    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 分页查询设备列表
     * @param vmQueryListDto
     * @return
     */
    @Override
    public PageResult<VendingMachineVo> list(VmQueryListDto vmQueryListDto) {
        //开启分页
        PageHelper.startPage(vmQueryListDto.getPageNum(), vmQueryListDto.getPageSize());
        //查询点位列表
        List<VendingMachineVo> vmList =  vmMapper.list(vmQueryListDto);
        //获取分页详情对象
        PageInfo<VendingMachineVo> pageInfo = new PageInfo<>(vmList);
        //封装数据返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 新增设备类型
     * @param vmAddDto
     * @return
     */
    @Override
    @Transactional
    public Integer add(VmAddDto vmAddDto) {
        //利用工具类生成8位随机的编号
        String innerCode = UUIDUtils.getUUID();
        //根据设备类型id查询设备
        VmType vmType = vmTypeMapper.getVmTypeById(vmAddDto.getVmTypeId());
        //根据点位id查询点位
        Node node = nodeMapper.getNodeById(vmAddDto.getNodeId());
        //拼接详情地址
        String detailAddr = node.getNodeName() + vmAddDto.getAddr();
        //为设备的其他属性段赋值并插入
        VendingMachine vendingMachine = new VendingMachine();
        BeanUtils.copyProperties(vmAddDto, vendingMachine);
        BeanUtils.copyProperties(node, vendingMachine);
        vendingMachine.setInnerCode(innerCode);
        vendingMachine.setAddr(detailAddr);
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        vendingMachine.setVmStatus(StatusConstant.NOT_FOR_USE);
        vendingMachine.setRunningStatus(StatusConstant.VM_NORMAL);
        vendingMachine.setLastSupplyTime(StatusConstant.DEFAULT_LAST_SUPPLY_TIME);
        Integer rows = vmMapper.add(vendingMachine);
        //计算货道数量
        int capacity = (int) (vmType.getVmRow() * vmType.getVmCol());
        //根据设备的行与列构造货道编号
        List<Channel> channelList = new ArrayList<>(capacity);
        for (int row = 1; row <= vmType.getVmRow(); row++) {
            for (int col = 1; col <= vmType.getVmCol(); col++) {
                Channel channel = Channel.builder()
                        .innerCode(innerCode)
                        .vmId(vendingMachine.getId())
                        .currentCapacity(StatusConstant.CHANNEL_DEFAULT_CAPACITY)
                        .maxCapacity(vmType.getChannelMaxCapacity())
                        .build();
                channel.setChannelCode(row + "-" + col);
                channelList.add(channel);
            }
        }
        //批量插入货道
        channelMapper.insertBatch(channelList);
        return rows;
    }

    /**
     * 根据id查询设备
     * @param id
     * @return
     */
    @Override
    public VendingMachine getVmById(Long id) {
        return vmMapper.getVmById(id);
    }

    /**
     * 修改设备
     * @param vendingMachine
     * @return
     */
    @Override
    public Integer edit(VendingMachine vendingMachine) {
        return vmMapper.edit(vendingMachine);
    }

    /**
     * 根据id删除设备
     * @param ids
     * @return
     */
    @Override
    public Integer deleteVmById(Long[] ids) {

        return vmMapper.deleteVmById(ids);
    }

}
